package com.leo.manage_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leo.manage_system.entity.PageView;
import com.leo.manage_system.entity.UserWechat;
import com.leo.manage_system.mapper.PageViewMapper;
import com.leo.manage_system.service.PageViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 访问量 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-28
 */
@Service
public class PageViewServiceImpl extends ServiceImpl<PageViewMapper, PageView> implements PageViewService {
    @Autowired
    PageViewMapper pageViewMapper;
    private int sum ;
    @Override
    public int getViewByDate(Date date) {
        QueryWrapper<PageView> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("date",getDate(date,1));//小于
        queryWrapper.ge("date",date);//大于
        return pageViewMapper.selectOne(queryWrapper).getView();
    }


    @Override
    public List<Integer> getPageView(Date date) {
        sum = 0;
//        Date date = new Date(); // this object contains the current date value
        date = getDate(date,-6);
        List<Integer> list = new ArrayList<>();
//        Map<String,Object> map = new HashMap<>();
        for(int i = 0; i <= 6; i++){

            Date temp_date = getDate(date,1);
            int temp = DateApart(temp_date,date);
            sum += temp;
            date=temp_date;
            list.add(temp);
        }

        return list;
    }
    @Override
    public int getSum() {
        return sum;
    }
    public int DateApart(Date date1,Date date2){
        QueryWrapper<PageView> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("date",date1);//小于
        queryWrapper.ge("date",date2);//大于
        if (pageViewMapper.selectOne(queryWrapper)!=null){
            return pageViewMapper.selectOne(queryWrapper).getView();
        }else{
            return 0;
        }

    }
    public Date getDate(Date date,int num){
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DATE,num);
        Date date2=rightNow.getTime();
        return date2;
    }
}
